package com.wyy.mall.tiny.service.impl;

import com.wyy.mall.tiny.common.api.CommonResult;
import com.wyy.mall.tiny.component.CancelOrderSender;
import com.wyy.mall.tiny.dto.OrderParam;
import com.wyy.mall.tiny.service.OmsPortalOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * created by 伍猷煜 on 2021/5/25 14:47 星期二
 */
@Service
public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {

    private static Logger LOGGER = LoggerFactory.getLogger(OmsPortalOrderServiceImpl.class);

    private final CancelOrderSender mCancelOrderSender;

    @Value("${mall.order.delayTimes}")
    private Long delayTimes;

    @Autowired
    public OmsPortalOrderServiceImpl(CancelOrderSender cancelOrderSender) {
        mCancelOrderSender = cancelOrderSender;
    }

    @Override
    public CommonResult generateOrder(OrderParam orderParam) {
        // TODO 执行一系列下单操作
        LOGGER.info("process generateOrder");
        // 下单完成后开启一个延迟消息，用于当用户没有付款时取消订单
        sendDelayMessageCancelOrder((long)new Random().nextInt(1001));
        return CommonResult.success(null, "下单成功");
    }

    @Override
    public void cancelOrder(Long orderId) {
        // TODO 执行一系列取消订单操作
        LOGGER.info("process cancelOrder orderID:{}", orderId);
    }

    private void sendDelayMessageCancelOrder(Long orderId) {
        // 实际业务60分钟，这里测试用
        // long delayTimes = 30 * 1000;
        mCancelOrderSender.sendMessage(orderId, delayTimes);
    }
}
